package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.console.service.facade.PrdInfoMapService;
import com.irdstudio.efp.console.service.vo.PrdInfoMapVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class PrdInfoMapController extends AbstractController {

	@Autowired
	@Qualifier("prdInfoMapService")
	private PrdInfoMapService prdInfoMapService;
	@Autowired
	private PatternedLimitableSeqService sequenceService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/prd/info/maps", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdInfoMapVO>> queryPrdInfoMapAll(@RequestBody PrdInfoMapVO vo) {
		// 根据用户角色获取用户权限方案
		List<PrdInfoMapVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = prdInfoMapService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = prdInfoMapService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = prdInfoMapService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = prdInfoMapService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/prd/info/map/", method = RequestMethod.GET)
	public @ResponseBody ResponseData<PrdInfoMapVO> queryByPk() {
		PrdInfoMapVO inVo = new PrdInfoMapVO();
		PrdInfoMapVO outputVo = prdInfoMapService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param prdInfoMap
	 * @return
	 */
	@RequestMapping(value = "/prd/info/map", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdInfoMapVO inPrdInfoMapVo) {
		int outputVo = prdInfoMapService.deleteByPk(inPrdInfoMapVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inPrdInfoMapVo
	 * @return
	 */
	@RequestMapping(value = "/prd/info/map", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdInfoMapVO inPrdInfoMapVo) {
		int outputVo = prdInfoMapService.updateByPk(inPrdInfoMapVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inPrdInfoMapVo
	 * @return
	 */
	@RequestMapping(value = "/prd/info/map", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertPrdInfoMap(@RequestBody PrdInfoMapVO inPrdInfoMapVo) {
		// 获取流水号
		String serno = "";
		try {
			serno = sequenceService.getPatternedSequence("PRD_INFO_MAP_SERNO", "SEQ", 5);
		} catch (Exception e) {
			logger.debug("获取流水号失败!" + e);
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setMessage("生成流水号失败！");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}
		inPrdInfoMapVo.setSerno(serno);
		int outputVo = prdInfoMapService.insertPrdInfoMap(inPrdInfoMapVo);
		return getResponseData(outputVo);

	}
}
